package com.seuic.hnjiningposapp.hnapps;


import androidx.multidex.MultiDexApplication;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.Utils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.model.HttpHeaders;
import com.seuic.hnjiningposapp.hnapps.util.Api;
import com.seuic.hnjiningposapp.hnapps.util.CrashHandler;
import com.seuic.hnjiningposapp.hnapps.util.FTPUtils;
import com.seuic.posdsdk.pos_func.PosFuncs;
import com.socks.library.KLog;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * @author rainking
 */
public class PosApp extends MultiDexApplication {

    private static final String TAG = "PosApp";
    private static PosApp mInstance;

    public static synchronized PosApp getInstance() {
        return mInstance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        Utils.init(mInstance);
        KLog.init(true, "PosApp");
        initOkGo();
        PosFuncs.getInstace().init(this);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        String Ztime = SPStaticUtils.getString("ztime", "第一次打开APP");
        if (Ztime.equals("第一次打开APP")) {
            SPStaticUtils.put("ztime", simpleDateFormat.format(date));
        } else {
            if (!Ztime.equals(simpleDateFormat.format(date))) {
                SPStaticUtils.put("ztime", simpleDateFormat.format(date));
                SPStaticUtils.put("ccsno", 1);
            }
        }
        CrashHandler.getInstance().init(this);
        if (SPStaticUtils.getString("FTPIP").equals("")) {
            SPStaticUtils.put("FTPIP", "192.168.1.1");
            SPStaticUtils.put("FTPCOMPORT", 21);
            SPStaticUtils.put("FTPUSERNAME", "ftproot");
            SPStaticUtils.put("FTPUSEPASSWORD", "netadmin");
        }

        FTPUtils.getInstance().initFtpClient(
                SPStaticUtils.getString("FTPIP"),
                SPStaticUtils.getInt("FTPCOMPORT"),
                SPStaticUtils.getString("FTPUSERNAME"),
                SPStaticUtils.getString("FTPUSEPASSWORD"));
    }

    private void initOkGo() {

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //log相关
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("Pos");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);


        //超时时间设置，默认60秒
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);

        builder.cookieJar(new CookieJarImpl(new SPCookieStore(this)));

        //https相关设置，以下几种方案根据需要自己设置
        //方法一：信任所有证书,不安全有风险
        HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
        //配置https的域名匹配规则，详细看demo的初始化介绍，不需要就不要加入，使用不当会导致https握手失败
        builder.hostnameVerifier((hostname, session) -> true);


        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put("Content-Type", "application/json; charset=GB18030;");
        httpHeaders.put("Accept", "application/json");
        // 其他统一的配置
        // 详细说明看GitHub文档：https://github.com/jeasonlzy/
        OkGo.getInstance().init(this)
                .setOkHttpClient(builder.build())
                .setCacheMode(CacheMode.NO_CACHE)
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .addCommonHeaders(httpHeaders)
                .setRetryCount(3);
        Charset.isSupported("X-UTF-32LE-BOM");
    }

    private void exitApp() {
        PosFuncs.getInstace().close();
        AppUtils.exitApp();
    }


}
